import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop/eventbus/event_bus.dart';
import 'package:flutter_shop/pages/category/category_tab_page/provider/left_menu_provider.dart';
import 'package:flutter_shop/pages/category/category_tab_page/widget/left_menu.dart';
import 'package:flutter_shop/pages/common/index_page/index_page.dart';
import 'package:flutter_shop/pages/home/home_tab_page/entity/home_content_data.dart';
import 'package:flutter_shop/r.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/widget/load_image.dart';
import 'package:flutter_shop/widget/water_click.dart';
import 'package:provider/provider.dart';

///分类导航
class CategoryBar extends StatelessWidget {
  final List<HomeContentDataCategory> categories;

  CategoryBar({Key key, this.categories}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: GridView.builder(
        shrinkWrap: true,
        itemCount: categories.length,
        physics: NeverScrollableScrollPhysics(),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 5,
        ),
        itemBuilder: _buildItem,
      ),
    );
  }

  ///构建item
  Widget _buildItem(context, index) {
    HomeContentDataCategory item = categories[index];

    return Container(
      child: WaterClick(
        onPressed: () {
          //跳转到分类tab的指定分类
          EventBus().post(EventMessage(IndexPage.CHANGE_INDEX, int1: 1));
          //通知切换左菜单
          EventBus().post(EventMessage(LeftMenu.CHANGE_INDEX, int1: index));
          //因为在分类页还没初始化时,eventbus收不到消息,所以这里同时手动设置左菜单索引,以保证在分类页一加载就可以改变切换
          Provider.of<LeftMenuProvider>(context, listen: false).changeIndex(index);
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            LoadImage(
              item.image,
              fit: BoxFit.fill,
              width: ScreenUtil().setWidth(80),
              height: ScreenUtil().setWidth(80),
            ),
            Gaps.v_gap_5,
            Text(
              item.mallCategoryName,
              style: TextStyles.second_text,
            ),
          ],
        ),
      ),
    );
  }
}
